/*
 * Copyright (c) MengChang Wang <wangmengchang@gmail.com>
 * Webpage:  http://www.journalhome.com/texpen/
 *
 *
*/

#include "../headers/runthread.h"
#include <QDebug>

RunThread::RunThread(QObject *parent) :
    QThread(parent)
{
    proc = NULL;
    prog ="";
    dir ="";
    arglist.clear();
}

void RunThread::run (){
    qDebug()<<"now run()...";
    if(prog.isEmpty()){
        return;
    }
    if(proc!=NULL){
        if(proc->state()==QProcess::Running){
            if(!proc->waitForFinished()){
                qDebug()<<"A proc is running, now terminate() it...";
                proc->terminate();
            }
        }
        delete proc;proc=NULL;
    }

    proc = new QProcess;
    connect(proc,SIGNAL(readyRead()),wnd,
            SLOT(readProcOutput())
            );
    connect(proc, SIGNAL(finished(int)),
            this, SLOT(onFinish(int))
            );

    //connect(proc, SIGNAL(stateChanged(QProcess::ProcessState)),
    //        this, SLOT(onState(QProcess::ProcessState))
    //        );

    connect(proc, SIGNAL(readyReadStandardOutput()),
            this, SLOT(onReadStd())
            );

    connect(proc, SIGNAL(readChannelFinished()),
            this, SLOT(onReadChannel())
            );
    connect(proc, SIGNAL(error(QProcess::ProcessError)),this,SLOT(onError(QProcess::ProcessError)));

    //qDebug()<<prog<<arglist<<endl;
    if(arglist.isEmpty()){
        proc->setWorkingDirectory(dir);
        proc->start(prog);
        //QByteArray ba = prog.toUtf8();
        //system(ba.data());
    }else{
        proc->start(prog,arglist);
    }
}

bool RunThread::isCmdRunning(){
    if(proc==NULL){
        return false;
    }
    if(proc->state() == QProcess::Running || proc->state() ==QProcess::Starting){
        return true;
    }
    return false;
}

void RunThread::read(){
    qDebug()<<"RunThread::read() called."<<endl;
    qDebug()<< proc->readAll()<<endl;
}

void RunThread::onReadStd(){
    qDebug()<<"RunThread::onReadStd() called."<<endl;
    qDebug()<< proc->readAllStandardOutput()<<endl;
}

void RunThread::onReadChannel(){
    qDebug()<<"RunThread::onReadChannel() called."<<endl;
    qDebug()<< proc->readChannel()<<endl;
}

void RunThread::onFinish(int ec){
    if(wnd!=NULL){
        wnd->onCmdFinish();
    }
    qDebug()<<"Finished, exit code ="<<ec<<endl;
}

/*
void RunThread::onState(QProcess::ProcessState st){
    if(st == QProcess::NotRunning){
        qDebug()<<"Proc is not running.."<<endl;
    }
    else if(st == QProcess::Starting){
        qDebug()<<"Proc is try to start.."<<endl;
    }else if(st == QProcess::Running){
        qDebug()<<"Proc is now running.."<<endl;
    }else{
        qDebug()<<"Proc is doing what? Even the system does not know..."<<endl;
    }
}
*/

void RunThread::setProg(QString& p){
    prog = p;
}
void RunThread::setDir(QString& d){
    dir = d;
}

void RunThread::addArg(QString& ag){
    arglist.append(ag);
}

void RunThread::reset(){
    prog="";
    arglist.clear();
}

void RunThread::onError(QProcess::ProcessError er){
    switch(er){
        case QProcess::FailedToStart:
            qDebug()<<"[Error] RunThread: FailedToStart"<<endl;
            break;
        case QProcess::Crashed:
            qDebug()<<"[Error] RunThread: Crashed"<<endl;
            break;
        case QProcess::Timedout:
            qDebug()<<"[Error] RunThread: Timed Out"<<endl;
            break;
        case QProcess::WriteError:
            qDebug()<<"[Error] RunThread: Write Error"<<endl;
            break;
        case QProcess::ReadError:
            qDebug()<<"[Error] RunThread: Read Error"<<endl;
            break;
        case QProcess::UnknownError:
            qDebug()<<"[Error] RunThread: Unknown Error"<<endl;
            break;
        default:
            break;
    }

}

